package com.tt.job.api;

import com.tt.job.api.schedule.JobSchedule;
import org.springframework.core.AttributeAccessorSupport;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.Map;

/**
 * job执行时的上下文信息
 */
public class JobContext extends AttributeAccessorSupport {

    private String jobClass;

    private Map<String, String> jobParameters;
    /**
     * 当前任务的触发信息
     */
    private JobSchedule schedule;

    /**
     * 此次调度的实际触发时间（可能由于调度服务器负载高，事件可能延迟一点时间调度）
     */
    private Date fireTime;

    /**
     * 此次调度的规划调度时间（不考虑调度服务的情况，规划的调度时间）
     */
    private Date scheduledFireTime;
    /**
     * 上一次事件触发时间
     */
    private Date previousFireTime;
    /**
     * 下一次事件触发时间
     */
    private Date nextFireTime;

    private int retryTimes;

    public JobContext(){

    }

    public JobContext(String jobClass, JobSchedule schedule){
        this.jobClass = jobClass;
        this.schedule = schedule;
    }

    public String getJobClass() {
        return jobClass;
    }

    public void setJobClass(String jobClass){
        this.jobClass = jobClass;
    }

    /**
     * 获取当前任务被调度
     */
    public JobSchedule getSchedule() {
        return schedule;
    }

    public void setSchedule(JobSchedule schedule){
        this.schedule = schedule;
    }

    /**
     * 此次调度的实际触发时间（可能由于调度服务器负载高，事件可能延迟一点时间调度）
     * @return
     */
    public Date getFireTime() {
        return fireTime;
    }

    public void setFireTime(Date fireTime) {
        this.fireTime = fireTime;
    }

    /**
     * 此次调度的规划调度时间（不考虑调度服务的情况，规划的调度时间）
     * @return
     */
    public Date getScheduledFireTime() {
        return scheduledFireTime;
    }

    public void setScheduledFireTime(Date scheduledFireTime) {
        this.scheduledFireTime = scheduledFireTime;
    }

    /**
     * 上一次事件触发时间
     * @return
     */
    public Date getPreviousFireTime() {
        return previousFireTime;
    }

    public void setPreviousFireTime(Date previousFireTime) {
        this.previousFireTime = previousFireTime;
    }

    /**
     * 下一次事件触发时间
     * @return
     */
    public Date getNextFireTime() {
        return nextFireTime;
    }

    public void setNextFireTime(Date nextFireTime) {
        this.nextFireTime = nextFireTime;
    }

    /**
     * 此次调度的重试次数
     * @return
     */
    public int getRetryTimes() {
        return retryTimes;
    }

    public void setRetryTimes(int retryTimes) {
        this.retryTimes = retryTimes;
    }

    /**
     * job的这一次执行的参数信息
     * @return
     */
    public Map<String, String> getJobParameters() {
        return jobParameters;
    }

    public void setJobParameters(Map<String, String> jobParameters) {
        this.jobParameters = jobParameters;
    }

    /**
     * 获取指定的参数，方法优先从context中取值，没有再从schedule中取值
     * @param name
     * @return null/具体的值
     */
    public String getParameter(String name){
        String value = null;
        if (jobParameters != null){
            value = jobParameters.get(name);
        }
        if (StringUtils.isEmpty(value) && schedule != null && schedule.getJobParameters() != null){
            value = schedule.getJobParameters().get(name);
        }
        return value;
    }
}
